17. 电话号码的字母组合
为保证权益,题目请参考 17. 电话号码的字母组合(From LeetCode).
解决方案1
CPP
C++
#include <iostream>
#include <vector>
#include <string>
using namespace std;
class Solution
{
public:
vector<string> ans;
int n;
string digitss;
vector<string> letterCombinations(string digits)
{
n = digits.length();
if (n == 0)
{
return ans;
}
digitss = digits;
helper(0, "");
return ans;
}
void helper(int i, string tmp)
{
if (i == n)
{
ans.push_back(tmp);
}
else
{
string basic = "";
switch (digitss[i])
{
case '2':
basic = "abc";
break;
case '3':
basic = "def";
break;
case '4':
basic = "ghi";
break;
case '5':
basic = "jkl";
break;
case '6':
basic = "mno";
break;
case '7':
basic = "pqrs";
break;
case '8':
basic = "tuv";
break;
case '9':
basic = "wxyz";
break;
default:
basic = "";
break;
}
for (char ch : basic)
{
helper(i + 1, tmp + ch);
}
}
}
};
int main()
{
Solution so;
for (string x : so.letterCombinations("7"))
{
cout << x << endl;
}
return 0;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80